//
//  TranslateDic.m
//  huashiedu
//
//  Created by oceans on 2018/9/5.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "TranslateDic.h"
#import "DateformateShared.h"

@implementation TranslateDic

+ (NSDictionary *)momentDic:(NSDictionary *)rawDic {
    
    NSDictionary *dic = @{
                          @"approve": [NSNumber numberWithInt:0],
                          @"auditstatus": [NSNumber numberWithInt:0],
                          @"compid": [NSNumber numberWithInt:0],
                          @"draft": [NSNumber numberWithInt:0],
                          @"dsp": [rawDic objectForKey:@"content"],
                          @"exttime": [[DateformateShared shared] timeintervalToString:[[rawDic objectForKey:@"time"] doubleValue]],
                          @"issue": [NSNumber numberWithInt:0],
                          @"nick": [[rawDic objectForKey:@"user"] objectForKey:@"nickname"],
                          @"optcomment": [self discussArray:[rawDic objectForKey:@"comments"]],
                          @"optthumb": [self likeArr:[rawDic objectForKey:@"likes"]],
                          @"pagetype": [NSNumber numberWithInt:0],
                          @"photocollections": [rawDic objectForKey:@"pics"],
                          @"pid": [NSNumber numberWithInt:[[rawDic objectForKey:@"id"] intValue]],
                          @"portrait": [[rawDic objectForKey:@"user"] objectForKey:@"avatar"],
                          @"remark": @"",
                          /*@"rownum_":*/
                          @"spreadparams": @{},
                          @"thumb": @"",
                          @"title": [rawDic objectForKey:@"content"],
                          @"url": @"",
                          @"userid": [NSString stringWithFormat:@"%@", [[rawDic objectForKey:@"user"] objectForKey:@"id"]],
                          @"verify": [NSNumber numberWithInt:0],
                          @"version": [NSNumber numberWithInt:1]
                          };
    return dic;
}

+ (NSArray *)discussArray:(NSArray *)array {
    
    NSMutableArray *marr = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        
        NSDictionary *item = [self discussDic:dic];
        [marr addObject:item];
    }
    return marr;
}

+ (NSArray *)likeArr:(NSArray *)array {
    NSMutableArray *marr = [NSMutableArray arrayWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        
        NSDictionary *item = [self likeDic:dic];
        [marr addObject:item];
    }
    return marr;
}

+ (NSDictionary *)discussDic:(NSDictionary *)rawDic {
    
    NSString *nick = nil;
    if ([rawDic objectForKey:@"nickname"] == nil || (NSNull *)[rawDic objectForKey:@"nickname"] == [NSNull null]) {
        nick = @"";
    }else {
        nick = rawDic[@"nickname"];
    }
    NSDictionary *dic = @{
                          @"approve": [NSNumber numberWithInt:0],
                          @"nick": nick,
                          @"optid": @"",
                          @"opttype": [NSNumber numberWithInt:1],
                          @"portrait": @"",
                          @"remark": @"",
                          @"userid": [NSString stringWithFormat:@"%@", [rawDic objectForKey:@"id"]],
                          @"message": [self emojiDecode:[rawDic objectForKey:@"content"]]
                          };
    return dic;
}

+ (NSString *)emojiDecode:(NSString *)dstring {
    const char *jsonString = [dstring UTF8String];
    NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
    NSString *good = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
    if (good == nil) {
        return dstring;
    }
    return good;
}

+ (NSDictionary *)likeDic:(NSString *)raw {
    
    NSDictionary *dic = @{
                          @"approve": [NSNumber numberWithInt:0],
                          @"nick": raw,
                          @"optid": @"",
                          @"opttype": [NSNumber numberWithInt:0],
                          @"portrait": @"",
                          @"remark": @"",
                          @"userid": @""
                          };
    return dic;
}

@end
